package com.lipuwater.agent.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.ImageView;

import com.google.zxing.WriterException;
import com.lipuwater.agent.R;

import io.github.xudaojie.qrcodelib.BitmapUtil;


public class QRCodeDialog extends Dialog {
    private Context mContext;

    public QRCodeDialog(Context context) {
        this(context, "http://weixin.qq.com/r/sD-QyKfENvpJreOr92qi");
    }

    public QRCodeDialog(Context context, String str) {
        super(context, R.style.MyDialogStyle);
        setContentView(R.layout.dialog_qrcode);
        this.mContext = context.getApplicationContext();
        initDialog(str);
    }

    private ImageView Image;

    private void initDialog(String str) {
        Image = (ImageView) findViewById(R.id.Image);
        try {
            Image.setImageBitmap(BitmapUtil.createQRCode(str, dpToPx(230, mContext)));
        } catch (WriterException e) {
            e.printStackTrace();
        }
        setCanceledOnTouchOutside(true);
        getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT/* dpToPx(100, mContext)*/);
    }


    private int dpToPx(int i, Context mContext) {
        DisplayMetrics displayMetrics = mContext.getResources()
                .getDisplayMetrics();
        return (int) ((i * displayMetrics.density) + 0.5);
    }
}
